package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.WxItemRecord;
import com.wxxymakers.grademark.model.entity.WxUser;
import com.wxxymakers.grademark.service.WxIncidentService;
import com.wxxymakers.grademark.service.WxItemRecordService;
import com.wxxymakers.grademark.utils.Const;
import com.wxxymakers.grademark.utils.NumUtils;
import com.wxxymakers.grademark.utils.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR soft
 * @DATE 2018/12/22 21:30
 * @DESCRIBE
 */
@Slf4j
@RestController
@RequestMapping("/item/record")
public class ItemRecordController {

    @Autowired
    private WxItemRecordService recordService;

    @Autowired
    private WxIncidentService incidentService;

    @PostMapping("/add")
    public RestResponse add(WxItemRecord record,
                            @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        record.setUserId(user.getId()); // 设置记录所属用户
        if (recordService.add(record)) {
            return RestResponse.ok(record);
        }
        return RestResponse.fail();
    }

    /**
     * 只允许删除自己的记录
     */
    @PostMapping("/delete/{id}")
    public RestResponse delete(@PathVariable int id,
                               @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        WxItemRecord record = recordService.byId(id);
        if (record == null || !record.getUserId().equals(user.getId())) {
            log.warn("非法删除：{}", user.getName());
            return RestResponse.fail("非法删除！警告一次！");
        }

        if (NumUtils.lteZero(record.getIncidentId()) || incidentService.isEnd(record.getIncidentId())) {
            return RestResponse.fail("当前事件已结束！无法删除！");
        }

        if (recordService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 只允许修改自己的记录
     */
    @PostMapping("/change")
    public RestResponse change(WxItemRecord record,
                               @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        WxItemRecord wxrd = recordService.byId(record.getId());
        if (wxrd == null || !wxrd.getUserId().equals(user.getId())) {
            log.warn("非法修改：{}", user.getName());
            return RestResponse.fail("非法修改！警告一次！");
        }

        /*if (wxrd.getStatus().equals("1")) {
            return RestResponse.fail("被统计过的记录无法修改！"); 加次功能就需要添加重新统计功能
        }*/

        if (NumUtils.lteZero(record.getIncidentId()) || incidentService.isEnd(record.getIncidentId())) {
            return RestResponse.fail("当前事件已结束！无法修改！");
        }

        record.setUserId(user.getId());
        if (recordService.change(record)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 管理员的调整 一般是更改分数
     * 没使用
     * @param record 记录
     */
    @PostMapping("/adjust")
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    public RestResponse adjustRecord(WxItemRecord record) {
        if (recordService.adjust(record)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 记录审核
     * @param dit 1通过 0不通过
     * @param id 被审核的记录
     */
    @PostMapping("/audit/{dit}")
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    public RestResponse audit(@PathVariable int dit, @RequestParam int id) {
        WxItemRecord record = new WxItemRecord();
        record.setId(id);
        record.setAudit(dit == 1);
        if (recordService.audit(record)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 查询一条记录
     * @param id 记录id
     */
    @PostMapping("/get/{id}")
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    public RestResponse get(@PathVariable int id) {
        WxItemRecord record = recordService.byId(id);
        if (record != null) {
            return RestResponse.ok(record);
        }
        return RestResponse.fail();
    }

    // 不是超级管理员则只能看到自己部门的用户记录
    @PostMapping("/page")
    @RequiresRoles(value = {"admin", "college-admin"}, logical = Logical.OR)
    public RestResponse page(int page, int limit, WxItemRecord record,
                             @RequestParam(defaultValue = "", required = false) Integer collegeId,
                             @RequestParam(defaultValue = "", required = false) Integer deptId,
                             @RequestParam(defaultValue = "", required = false) String username, // 需要查询的用户名
                             @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        if (!user.getRole().getSign().equals("admin")) {
            collegeId = user.getCollageId();  // 不是超级管理只能看到自己学院的记录
            record.setUserId(user.getId()); // 不是超级管理查询的评定记录要排除自己
        }
        Page<WxItemRecord> records = recordService.page(page, limit, collegeId, deptId, username, record);
        if (records != null) {
            return RestResponse.ok(records);
        }
        return RestResponse.fail();
    }

    /**
     * 查看自己的记录
     */
    @PostMapping("/self")
    public RestResponse self(int page, int limit, WxItemRecord record,
                             @SessionAttribute(Const.CURRENT_USER) WxUser user) {
        record.setUserId(user.getId());
        Page<WxItemRecord> records = recordService.page(page, limit, record);
        if (records != null) {
            return RestResponse.ok(records);
        }
        return RestResponse.fail();
    }
}
